package gugga.utils
{
	import flash.net.XMLSocket;
	
	import gugga.collections.dictionary.Dictionary;
	
	public class SOSTraceUtil
	{
		private static var SOS_TRACE_PORT : Number = 4444;
		private static var SOS_COMMAND_PORT : Number = 4445;
		
		private static var mSOSTraceSocket : XMLSocket;
		private static var mSOSCommandSocket : XMLSocket;
	
		private static var mSocketsInitialized : Boolean = false;
		
		private static var mKeyColors : Dictionary;
		
		private static function initializeSockets() : void
		{
			if(!mSocketsInitialized)
			{
				mSOSTraceSocket = new XMLSocket();
				mSOSTraceSocket.connect("localhost", SOS_TRACE_PORT);
				
				mSOSCommandSocket = new XMLSocket();
				mSOSCommandSocket.connect("localhost", SOS_COMMAND_PORT);
				
				mSocketsInitialized = true;
			}
		}
		
		public static function trace(aMessage : String) : void
		{
			initializeSockets();
			mSOSCommandSocket.send("<showMessage><![CDATA[" + aMessage + "]]></showMessage>");
		}
		
		public static function traceWithTime(aMessage : String) : void
		{
			initializeSockets();
			
			var now : Date = new Date;
			var currentTime : String = now.seconds +":" + now.milliseconds;
			
			
			mSOSCommandSocket.send("<showMessage><![CDATA[" + aMessage + currentTime + "]]></showMessage>");
		}
		
		public static function traceMessageWithKey(aMessage : String, aKeyName : String) : void
		{
			initializeSockets();
			mSOSCommandSocket.send("<showMessage key='" + aKeyName + "'><![CDATA[" + aMessage + "]]></showMessage>");
		}
				
		public static function traceFoldedMessage(aTitle : String, aMessage : String) : void
		{
			initializeSockets();
			
			mSOSCommandSocket.send(
				"<showFoldMessage>" +
				"	<title><![CDATA[" + aTitle + "]]></title>" +
				"	<message><![CDATA[" + aMessage + "]]></message>" +
				"</showFoldMessage>" 
			);
		}
		
		public static function setKeyColor(aKeyName : String, aColor:Number) : void
		{
			mKeyColors[aKeyName] = aColor;
			
			mSOSCommandSocket.send(
				"<setKey>" +
				"	<name>" + aKeyName + "</name>" +
				"	<color>" + aColor + "</color>" +
				"</setKey>\n"
			);
		}
		
		public static function traceFoldedMessageWithKey(aTitle : String, aMessage : String, aKeyName : String) : void
		{
			initializeSockets();
			
			mSOSCommandSocket.send(
				"<showFoldMessage key='" + aKeyName + "'>" +
				"	<title><![CDATA[" + aTitle + "]]></title>" +
				"	<message><![CDATA[" + aMessage + "]]></message>" +
				"</showFoldMessage>" 
			);
		}
		
		public static function isKeyColorSet(aKeyName : String) : Boolean
		{
			return mKeyColors.containsKey(aKeyName);
		}
	
		public static function getKeyColor(aKeyName : String) : Number
		{
			return mKeyColors[aKeyName];
		}	
	}
}